package com.example.servlet.order;

import com.example.entity.*;
import com.example.service.OrderService;
import com.example.service.impl.OrderServiceImpl;
import com.example.utils.MybatisUtil;
import com.example.utils.ThymeleafUtil;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.IOException;

@WebServlet("/user_order_check")
public class uOrderCheckServlet extends HttpServlet {
    private OrderService service;
    private SqlSession sqlSession;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        sqlSession = MybatisUtil.getSession();
        service = new OrderServiceImpl(sqlSession);
    }

    @Override
    public void destroy() {
        sqlSession.close();
        super.destroy();
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = (User) req.getSession().getAttribute("user");
        int o_id = Integer.parseInt(req.getParameter("o_id"));
        int f_id = Integer.parseInt(req.getParameter("f_id"));
        Context context = new Context();
        context.setVariable("user",user);
        context.setVariable("order", service.getOrderByOrderId(o_id));
        context.setVariable("passengers", service.getPassengerListByOrderId(o_id));
        context.setVariable("flight", service.getFlightByFlightId(f_id));
        context.setVariable("planes", service.getPlaneListByFId(f_id));
        context.setVariable("tickets", service.getTicketByOrderId(o_id));
        // 图片是否存在判断
        String imagePath = getServletContext().getRealPath("/static/images/" + user.getU_id() + ".jpg");
        File imageFile = new File(imagePath);
        boolean imageExists = imageFile.exists();
        context.setVariable("imageExists", imageExists);
        ThymeleafUtil.process("user_order_detail.html", context, resp.getWriter());
    }
}
